package com.third.customer.site.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.third.customer.model.dao.CompanyDao;
import com.third.customer.model.dao.param.CompanyParam;
import com.third.customer.model.data.JsonResult;
import com.third.customer.model.tables.Company;
import com.third.customer.service.utils.PageResult;
import com.third.customer.site.controller.resp.CompanyResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by hehuaichun on 2020/5/22.
 */
@RestController
@RequestMapping("/companies")
public class CompanyCtrl {

    @Autowired
    private CompanyDao companyDao;

    @GetMapping
    public JsonResult list(@RequestParam(required = false) String company,
                           @RequestParam(defaultValue = "0") Integer page,
                           @RequestParam(defaultValue = "10") Integer limit) {
        CompanyParam param = new CompanyParam();
        param.setLimit(limit);
        param.setPage(page);
        param.setName(company);
        Page<Company> page1 = companyDao.query(param);
        List<CompanyResp> respList = page1.getRecords().stream().map(CompanyResp::convert).collect(Collectors.toList());
        return JsonResult.ok(PageResult.build(page1, respList));
    }
}
